/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.sys.customerfeature.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.config.Global;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.sys.customerfeature.entity.CustomerFeature;
import cn.knet.sp.ncrm.modules.sys.customerfeature.service.CustomerFeatureService;
import cn.knet.sp.ncrm.modules.sys.utils.CustomerUtils;

/**
 * 客户特点Controller
 * @author zhangbin
 * @version 2015-08-25
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/customerFeature")
public class CustomerFeatureController extends BaseController {

	@Autowired
	private CustomerFeatureService customerFeatureService;

	/**
	 * 清除前后空格
	 * @author zhangbin
	 * @date 2015 /08/26 18:11:39
	 * void
	 * @param customerFeature the customer feature
	 */
	private void trim(CustomerFeature customerFeature) {
		if(customerFeature!=null&&StringUtils.isNotBlank(customerFeature.getName())){
			customerFeature.setName(StringUtils.trim(customerFeature.getName()));
		}
	}

	@ModelAttribute
	public CustomerFeature get(@RequestParam(required=false) String id) {
		CustomerFeature entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = customerFeatureService.get(id);
		}
		if (entity == null){
			entity = new CustomerFeature();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:customerFeature:view")
	@RequestMapping(value = {"list", ""})
	public String list(CustomerFeature customerFeature, HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/sys/customerfeature/customerFeatureAjaxList";
	}

	@RequiresPermissions("sys:customerFeature:view")
	@RequestMapping(value = "listAjaxData")
	@ResponseBody
	public Page listAjaxData(CustomerFeature customerFeature, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CustomerFeature> page = customerFeatureService.findPage(new Page<CustomerFeature>(request, response), customerFeature);
		return page;
	}

	@RequiresPermissions("sys:customerFeature:view")
	@RequestMapping(value = "form")
	public String form(CustomerFeature customerFeature, Model model) {
		customerFeature.addHistoryFlag();
		model.addAttribute("customerFeature", customerFeature);
		return "modules/sys/customerfeature/customerFeatureForm";
	}

	@RequiresPermissions("sys:customerFeature:edit")
	@RequestMapping(value = "save")
	public String save(CustomerFeature customerFeature, Model model, RedirectAttributes redirectAttributes) {
		this.trim(customerFeature);
		if (!beanValidator(model, customerFeature)){
			return form(customerFeature, model);
		}
		if(customerFeature.getIsNewRecord()){
			customerFeature.setDateCreated(new java.util.Date());
		}
		int count = customerFeatureService.findDistinctCustomerFeature(customerFeature);
		if(count>0){
			addMessage(model, "特点["+customerFeature.getName()+"]已经存在,请更换名称");
			return form(customerFeature, model);
		}
		customerFeature.setLastUpdated(new java.util.Date());//必加修改时间
		customerFeatureService.save(customerFeature);
		CustomerUtils.clearFeature(customerFeature); //修改也清空
		addMessage(redirectAttributes, "保存客户特点成功");
		return "redirect:"+Global.getAdminPath()+"/sys/customerFeature/?repage";
	}

	@RequiresPermissions("sys:customerFeature:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(CustomerFeature customerFeature, RedirectAttributes redirectAttributes) {
		AjaxMessage result = AjaxMessage.success();
		try {
			customerFeatureService.delete(customerFeature);
			CustomerUtils.clearFeature(customerFeature); //删除则清空缓存
			result.appendMessage("删除客户特点成功!");
		} catch (Exception e) {
			logger.warn("删除客户特点失败", e);
			result.makeError("删除客户特点失败!系统发生异常");
		}
		return result;
	}



}